home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / xml / etree / ElementPath.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  4KB  |  159 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5. xpath_tokenizer = re.compile('(::|\\.\\.|\\(\\)|[/.*:\\[\\]\\(\\)@=])|((?:\\{[^}]+\\})?[^/:\\[\\]\\(\\)@=\\s]+)|\\s+').findall
  6.  
  7. class xpath_descendant_or_self:
  8.     pass
  9.  
  10.  
  11. class Path:
  12.     
  13.     def __init__(self, path):
  14.         tokens = xpath_tokenizer(path)
  15.         self.path = []
  16.         self.tag = None
  17.         if tokens and tokens[0][0] == '/':
  18.             raise SyntaxError('cannot use absolute path on element')
  19.         
  20.         while tokens:
  21.             (op, tag) = tokens.pop(0)
  22.             if tag or op == '*':
  23.                 if not tag:
  24.                     pass
  25.                 self.path.append(op)
  26.             elif op == '.':
  27.                 pass
  28.             elif op == '/':
  29.                 self.path.append(xpath_descendant_or_self())
  30.                 continue
  31.             else:
  32.                 raise SyntaxError('unsupported path syntax (%s)' % op)
  33.             if tokens:
  34.                 (op, tag) = tokens.pop(0)
  35.                 if op != '/':
  36.                     if not op:
  37.                         pass
  38.                     raise SyntaxError('expected path separator (%s)' % tag)
  39.                 
  40.             op != '/'
  41.         if self.path and isinstance(self.path[-1], xpath_descendant_or_self):
  42.             raise SyntaxError('path cannot end with //')
  43.         
  44.         if len(self.path) == 1 and isinstance(self.path[0], type('')):
  45.             self.tag = self.path[0]
  46.         
  47.  
  48.     
  49.     def find(self, element):
  50.         tag = self.tag
  51.         if tag is None:
  52.             nodeset = self.findall(element)
  53.             if not nodeset:
  54.                 return None
  55.             
  56.             return nodeset[0]
  57.         
  58.         for elem in element:
  59.             if elem.tag == tag:
  60.                 return elem
  61.                 continue
  62.         
  63.  
  64.     
  65.     def findtext(self, element, default = None):
  66.         tag = self.tag
  67.         if tag is None:
  68.             nodeset = self.findall(element)
  69.             if not nodeset:
  70.                 return default
  71.             
  72.             if not nodeset[0].text:
  73.                 pass
  74.             return ''
  75.         
  76.         for elem in element:
  77.             if elem.tag == tag:
  78.                 if not elem.text:
  79.                     pass
  80.                 return ''
  81.                 continue
  82.         
  83.         return default
  84.  
  85.     
  86.     def findall(self, element):
  87.         nodeset = [
  88.             element]
  89.         index = 0
  90.         while None:
  91.             
  92.             try:
  93.                 path = self.path[index]
  94.                 index = index + 1
  95.             except IndexError:
  96.                 return nodeset
  97.  
  98.             set = []
  99.             if isinstance(path, xpath_descendant_or_self):
  100.                 
  101.                 try:
  102.                     tag = self.path[index]
  103.                     if not isinstance(tag, type('')):
  104.                         tag = None
  105.                     else:
  106.                         index = index + 1
  107.                 except IndexError:
  108.                     tag = None
  109.  
  110.                 for node in nodeset:
  111.                     new = list(node.getiterator(tag))
  112.                     if new and new[0] is node:
  113.                         set.extend(new[1:])
  114.                         continue
  115.                     set.extend(new)
  116.                 
  117.             else:
  118.                 for node in nodeset:
  119.                     for node in node:
  120.                         if path == '*' or node.tag == path:
  121.                             set.append(node)
  122.                             continue
  123.                     
  124.                 
  125.             if not set:
  126.                 return []
  127.             
  128.             nodeset = set
  129.             continue
  130.             return None
  131.  
  132.  
  133. _cache = { }
  134.  
  135. def _compile(path):
  136.     p = _cache.get(path)
  137.     if p is not None:
  138.         return p
  139.     
  140.     p = Path(path)
  141.     if len(_cache) >= 100:
  142.         _cache.clear()
  143.     
  144.     _cache[path] = p
  145.     return p
  146.  
  147.  
  148. def find(element, path):
  149.     return _compile(path).find(element)
  150.  
  151.  
  152. def findtext(element, path, default = None):
  153.     return _compile(path).findtext(element, default)
  154.  
  155.  
  156. def findall(element, path):
  157.     return _compile(path).findall(element)
  158.  
  159.